package com.gosophia.conferenceManagement.starting.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.conferenceManagement.starting.dao.ConferenceTransientStoreDAO;
import com.gosophia.conferenceManagement.starting.entity.ConferenceStartingTransientStore;
import com.gosophia.conferenceManagement.starting.service.ConferenceTransientStoreService;

/**
 * 会议发起暂存接口实现
 * 
 * @author tianchunyu
 */
@Service
public class ConferenceTransientStoreServiceImpl implements
		ConferenceTransientStoreService {

	@Autowired
	private ConferenceTransientStoreDAO conferenceTransientStoreDao;

	@Override
	public void deleteTransientInfo(
			ConferenceStartingTransientStore conferenceStartingTransientStore)
			throws BusinessException {
		Assert.isTrue(conferenceStartingTransientStore != null, "暂存实体不能为null");
		conferenceTransientStoreDao.delete(conferenceStartingTransientStore);

	}

	@Override
	public ConferenceStartingTransientStore findTransientInfoByCreator(
			Long userInfoId) {
		Assert.isTrue(userInfoId != null, "创建人编号不能为null");
		return conferenceTransientStoreDao
				.getTransientInfoByCreator((userInfoId));
	}

	@Override
	public ConferenceStartingTransientStore findTransientInfoByJobId(Long jobId) {
		Assert.isTrue(jobId != null, "关联任务编号不能为null");
		return conferenceTransientStoreDao.getUniqueByProperty("jobId", jobId);
	}

	@Override
	public ConferenceStartingTransientStore saveTransientInfo(
			ConferenceStartingTransientStore conferenceStartingTransientStore)
			throws BusinessException {
		Assert.isTrue(conferenceStartingTransientStore != null, "暂存实体不能为null");
		Assert.isTrue(
				conferenceStartingTransientStore.getTransientStoreId() == null,
				"实体id必须为null");
		Assert.notNull(conferenceStartingTransientStore.getConferenceCreator(),"会议发起人不能为空");
		conferenceTransientStoreDao.save(conferenceStartingTransientStore);
		ConferenceStartingTransientStore back = null;
		if (conferenceStartingTransientStore.getJobId() == null) {
			back = findTransientInfoByCreator(conferenceStartingTransientStore
					.getConferenceCreator().getUserInfoId());
		} else {
			back = findTransientInfoByJobId(conferenceStartingTransientStore
					.getJobId());
		}
		return back;
	}

	@Override
	public ConferenceStartingTransientStore updateTransientInfo(
			ConferenceStartingTransientStore conferenceStartingTransientStore)
			throws BusinessException {
		Assert.isTrue(conferenceStartingTransientStore != null, "暂存实体不能为null");
		Assert.isTrue(
				conferenceStartingTransientStore.getTransientStoreId() != null,
				"实体id不能为null");
		ConferenceStartingTransientStore transientEntity = conferenceTransientStoreDao
				.get(conferenceStartingTransientStore.getTransientStoreId());
		if(transientEntity == null){
			throw new BusinessException("数据读取错误","JC2_CON_B_1");
		}
		if (conferenceStartingTransientStore.getApplicationExplanation() != null) {
			transientEntity
					.setApplicationExplanation(conferenceStartingTransientStore
							.getApplicationExplanation());
		}
		if (conferenceStartingTransientStore.getApplicationInfo() != null) {
			transientEntity.setApplicationInfo(conferenceStartingTransientStore
					.getApplicationInfo());
		}
		if (conferenceStartingTransientStore.getConferenceCreator() != null) {
			transientEntity
					.setConferenceCreator(conferenceStartingTransientStore
							.getConferenceCreator());
		}
		if (conferenceStartingTransientStore.getConferenceDescription() != null) {
			transientEntity
					.setConferenceDescription(conferenceStartingTransientStore
							.getConferenceDescription());
		}
		if (conferenceStartingTransientStore.getConferenceModel() != null) {
			transientEntity.setConferenceModel(conferenceStartingTransientStore
					.getConferenceModel());
		}
		if (conferenceStartingTransientStore.getConferenceName() != null) {
			transientEntity.setConferenceName(conferenceStartingTransientStore
					.getConferenceName());
		}
		if (conferenceStartingTransientStore.getConferenceType() != null) {
			transientEntity.setConferenceType(conferenceStartingTransientStore
					.getConferenceType());
		}
		if (conferenceStartingTransientStore.getConfernceUnderTaker() != null) {
			transientEntity
					.setConfernceUnderTaker(conferenceStartingTransientStore
							.getConfernceUnderTaker());
		}
		if (conferenceStartingTransientStore.getImportantLevel() != null) {
			transientEntity.setImportantLevel(conferenceStartingTransientStore
					.getImportantLevel());
		}
		if (conferenceStartingTransientStore.getJobId() != null) {
			transientEntity.setJobId(conferenceStartingTransientStore
					.getJobId());
		}
		if (conferenceStartingTransientStore.getMajorIssue() != null) {
			transientEntity.setMajorIssue(conferenceStartingTransientStore
					.getMajorIssue());
		}
		if (conferenceStartingTransientStore.getMajorParticipators() != null) {
			transientEntity
					.setMajorParticipators(conferenceStartingTransientStore
							.getMajorParticipators());
		}
		if (conferenceStartingTransientStore.getNextApproval() != null) {
			transientEntity.setNextApproval(conferenceStartingTransientStore
					.getNextApproval());
		}
		if (conferenceStartingTransientStore.getOpinion() != null) {
			transientEntity.setOpinion(conferenceStartingTransientStore
					.getOpinion());
		}
		if (conferenceStartingTransientStore.getPlanEndTime() != null) {
			transientEntity.setPlanEndTime(conferenceStartingTransientStore
					.getPlanEndTime());
		}
		if (conferenceStartingTransientStore.getPlanStartTime() != null) {
			transientEntity.setPlanStartTime(conferenceStartingTransientStore
					.getPlanStartTime());
		}
		conferenceTransientStoreDao.update(transientEntity);
		return transientEntity;
	}

	@Override
	public ConferenceStartingTransientStore findTransientInfoById(
			Long transientInfoId) {
		Assert.isTrue(transientInfoId!=null,"transientInfoId不能为null");
		return conferenceTransientStoreDao.get(transientInfoId);
	}

	@Override
	public void deleteTransientInfoById(Long transientInfoId) {
		Assert.isTrue(transientInfoId!=null,"transientInfoId不能为null");
		conferenceTransientStoreDao.delete(transientInfoId);

	}

}
